package edu.exp3.model;

import java.util.ArrayList;

public class Course {
    private String id;
    private String name;
    private String place;
    private String time;     // 简化：用字符串表示
    private double credit;
    private int capacity;

    // 一门课一个教师
    private Teacher teacher;
    // ✅ 一门课允许多个学生
    private ArrayList<Student> students;

    public Course(String id, String name, String place, String time,
                  double credit, int capacity, Teacher teacher) {
        this.id = id;
        this.name = name;
        this.place = place;
        this.time = time;
        this.credit = credit;
        this.capacity = capacity;
        this.teacher = teacher;
        this.students = new ArrayList<Student>();
    }

    /** 学生选课（容量/重复校验 + 加入列表） */
    public boolean enroll(Student s) {
        if (s == null) return false;
        if (students.contains(s)) return false;          // 已选过
        if (students.size() >= capacity) return false;   // 容量满
        students.add(s);
        return true;
    }

    /** 学生退课（从列表移除） */
    public boolean drop(Student s) {
        if (s == null) return false;
        return students.remove(s);
    }

    // 只读访问器（演示 public）
    public String getId() { return id; }
    public String getName() { return name; }
    public String getPlace() { return place; }
    public String getTime() { return time; }
    public double getCredit() { return credit; }
    public int getCapacity() { return capacity; }
    public Teacher getTeacher() { return teacher; }

    /** 为了打印人数等，提供一个安全的只读视图（返回副本，避免外部直接改动内部列表） */
    public ArrayList<Student> getStudents() {
        return new ArrayList<Student>(students);
    }

    // 包内协作（演示 default 可见性）：不写修饰符，只在同包可见
    void setTeacherInternal(Teacher t) {
        this.teacher = t;
    }

    @Override
    public String toString() {
        return "课程[" + id + " " + name + "] 地点：" + place +
               " 时间：" + time + " 学分：" + credit + " 容量：" + capacity +
               " 授课教师：" + (teacher == null ? "未指定" : teacher.getName());
    }
}
